package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.StockInventoryMapper;
import com.ruoyi.wms.domain.StockInventory;
import com.ruoyi.wms.service.IStockInventoryService;

/**
 * 库存盘点Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-24
 */
@Service
public class StockInventoryServiceImpl implements IStockInventoryService {
    @Autowired
    private StockInventoryMapper stockInventoryMapper;

    /**
     * 查询库存盘点
     *
     * @param id 库存盘点主键
     * @return 库存盘点
     */
    @Override
    public StockInventory selectStockInventoryById(String id) {
        return stockInventoryMapper.selectStockInventoryById(id);
    }

    /**
     * 查询库存盘点列表
     *
     * @param stockInventory 库存盘点
     * @return 库存盘点
     */
    @Override
    public List<StockInventory> selectStockInventoryList(StockInventory stockInventory) {
        return stockInventoryMapper.selectStockInventoryList(stockInventory);
    }

    /**
     * 新增库存盘点
     *
     * @param stockInventory 库存盘点
     * @return 结果
     */
    @Override
    public int insertStockInventory(StockInventory stockInventory) {

        stockInventory.setId(MD5Utils.md5(stockInventory.getBatchNum()));
        stockInventory.setCreateUser(SecurityUtils.getUsername());
        stockInventory.setCreateDate(DateUtils.getCurrentTime());
        stockInventory.setUpdateDate( DateUtils.getCurrentTime());
        if(DateUtils.isValidDateTime( stockInventory.getTaskIssueTime())){
            stockInventory.setTaskIssueTime(DateUtils.getCurrentTime());
        }

        return stockInventoryMapper.insertStockInventory(stockInventory);
    }

    /**
     * 修改库存盘点
     *
     * @param stockInventory 库存盘点
     * @return 结果
     */
    @Override
    public int updateStockInventory(StockInventory stockInventory) {


        int updateResult = 0;
        if (StringUtils.isEmpty(stockInventory.getId())) {
            updateResult = insertStockInventory(stockInventory);
        }
        stockInventory.setUpdateDate( DateUtils.getCurrentTime());
        updateResult =stockInventoryMapper.updateStockInventory(stockInventory);
        if (0 == updateResult) {
            updateResult = insertStockInventory(stockInventory);
        }


        return updateResult;



    }

    /**
     * 批量删除库存盘点
     *
     * @param ids 需要删除的库存盘点主键
     * @return 结果
     */
    @Override
    public int deleteStockInventoryByIds(String[] ids) {
        return stockInventoryMapper.deleteStockInventoryByIds(ids);
    }

    /**
     * 删除库存盘点信息
     *
     * @param id 库存盘点主键
     * @return 结果
     */
    @Override
    public int deleteStockInventoryById(String id) {
        return stockInventoryMapper.deleteStockInventoryById(id);
    }
}
